<?php
/**
 * Image displayer.
 * 
 * @author Martin "DevelX" Jurča
 * @version 2009-03-19
 */
require_once '../config.php';
require_once '../libs/Image.php';
require_once '../libs/Cache.php';
require_once '../libs/FTP.php';

$params = array();
if (isset($_GET['url'])) {
    $params[] = array('cmd' => 'import', 0 => $_GET['url']);
}

if (isset($_GET['watermark'])) {
    $cmd = array('cmd' => 'watermark', 0 => $_GET['watermarkImage'], 1 =>
            $_GET['watermarkX'], 2 => $_GET['watermarkY'], 3 =>
            isset($_GET['watermarkOpacity']) ? $_GET['watermarkOpacity'] :
            100);
    $params[] = $cmd;
}

if (isset($_GET['resize'])) {
    $cmd = array('cmd' => 'resize', 0 => $_GET['resizeWidth'], 1 =>
            $_GET['resizeHeight']);
    if (isset($_GET['resizeBorder'])) {
        $cmd[2] = true;
        if (isset($_GET['resizeBorderColor'])) {
            $cmd[3] = array($_GET['resizeBorderColorR'],
                    $_GET['resizeBorderColorG'], $_GET['resizeBorderColorB']);
        }
    }
    if (isset($_GET['resizeDontKeepAspectRatio'])) {
        if (!isset($_GET['resizeBorder'])) {
            $cmd[2] = false;
            $cmd[3] = $_GET['resizeBorderColor'];
        }
        $cmd[4] = false;
    }
    $params[] = $cmd;
}

if (isset($_GET['rotate'])) {
    $params[] = array('cmd' => 'rotate', 0 => $_GET['rotateAngle'], 1 =>
            isset($_GET['rotateBackground']) ?
            array($_GET['rotateBackgroundR'], $_GET['rotateBackgroundG'],
            $_GET['rotateBackgroundB']) : -1);
}

if (isset($_GET['crop'])) {
    $params[] = array('cmd' => 'crop', 0 => $_GET['cropWidth'], 1 =>
            $_GET['cropHeight'], 2 => $_GET['cropX'], 3 => $_GET['cropY']);
}

if (isset($_GET['filter'])) {
    $params[] = array('cmd' => 'filter', 0 => $_GET['filterMethod'], 1 =>
            array(0 => $_GET['filter1'], 1 => $_GET['filter2'], 2 =>
            $_GET['filter2'], 3 => $_GET['filter3']));
}

if (isset($_GET['serialized'])) {
    $params = unserialize($_GET['serialized']);
}

$extenstion = strtolower(substr($params[0][0], -3));
$mime = array('jpg' => 'jpeg', 'gif' => 'gif', 'png' => 'png');
header('Content-Type: image/' . (isset($mime[$extenstion]) ? $mime[$extenstion] : 'jpeg'));

$image = &Cache::getImage($params);

echo $image;
?>
